# XJ通用工具类库编译
# LiXiujie
# 20151010

DIR_SRC = ./src
DIR_INC = ./include
DIR_LIB = ./lib
SRC = $(wildcard ${DIR_SRC}/*.cpp)  
## patsubst 模式字符串替换函数
OBJS = $(patsubst %.cpp,${DIR_SRC}/%.o,$(notdir ${SRC})) 

OBJS_TINYXML = ${DIR_SRC}/XJXMLProfile.o
OBJS_OCCI = ${DIR_SRC}/XJOCCI.o
OBJS_CURL = ${DIR_SRC}/XJCurl.o 
OBJS_CJSON = 

LocalDate = $(shell date +"%Y%m%d")
LocalTime = $(shell date +"%H%M%S")
LocalYear = $(shell date +"%y")
LocalMonth = $(shell date +"%m")
LocalDay = $(shell date +"%d")



RM = rm -fr
AR = ar crv
CC = g++
FLAG = -g -D__DEBUG__ -w $(CPPFLAGS) -liconv
SHARED = -shared 
##XJ_FPIC = -fPIC 


LIB_OBJECT_SO = libXJUtilClass.so
LIB_OBJECT_A = libXJUtilClass.a

TARGET = $(LIB_OBJECT_SO) $(LIB_OBJECT_A)

ifeq ($(ORACLE_INC),)
ORACLE_INC := ./
endif
ifeq ($(TINYXML_INCLUDE_DIR),)
TINYXML_INCLUDE_DIR := ./
endif
ifeq ($(CJSON_INCLUDE_DIR),)
CJSON_INCLUDE_DIR := ./
endif
ifeq ($(CJSON_LIBRARY_DIR),)
CJSON_LIBRARY_DIR := ./
endif
ifeq ($(TINYXML_LIBRARY_DIR),)
TINYXML_LIBRARY_DIR := ./
endif

LIB_PATH = -L/usr/lib64 -L/lib64 -L/usr/local/lib -L/usr/local/apr/lib\
  -L$(HOME)/lib -L$(HOME)/lib/public \
  -L$(TUXDIR)/lib \
  -L${HOME}/lib/json \
  -L$(ORACLE_HOME)/lib -L$(ORACLE_HOME)/precomp/lib \
  -L$(ORACLE_HOME)/rdbms/lib -L$(ORACLE_HOME)/sqlplus/lib \
  -L$(ORACLE_HOME)/network/lib -L$(ORACLE_HOME)/plsql/lib \
  -L$(CJSON_LIBRARY_DIR) \
  -L$(TINYXML_LIBRARY_DIR) \
  -L../lib -Llib
  
INC_PATH = -I${HOME}/include -I$(HOME)/include/public \
  -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo \
  -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/plsql/public \
  -I$(ORACLE_HOME)/include \
  -I$(ORACLE_INC) \
  -I$(TINYXML_INCLUDE_DIR) \
  -I$(CJSON_INCLUDE_DIR) \
  -I../include -I$(DIR_INC)
all:
	mkdir -p $(DIR_LIB)
	make show-objs
	-rm -f $(OBJS) $(DIR_SRC)/*.o
	make so
	-rm -f $(OBJS) $(DIR_SRC)/*.o
	make a
	@echo $(LIB_OBJECT_SO) "OK"
	@echo $(LIB_OBJECT_A) "OK"
help:
	@echo "======[XJ通用工具类库]======"	
	@echo "File_Encode:UTF8"
	@echo "编译静态库和动态库"
	@echo "用法: make"
	@echo "用法: make [disable-tinyxml] [disable-curl] [disable-occi] [disable-cjson] all"
	@echo ""
	@echo "Options:"
	@echo "    disable-tinyxml     disable tinyxml lib"
	@echo "    disable-curl        disable curl lib"
	@echo "    disable-occi        disable oracle occi lib"
	@echo "    disable-cjson       disable c-josn lib"
	@echo "    enable-tinyxml      enable tinyxml lib"
	@echo "    enable-curl         enable curl lib"
	@echo "    enable-occi         enable oracle occi lib"
	@echo "    enable-cjson        enable c-josn lib"
	@echo ""
	
disable-tinyxml:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_TINYXML/d .Makefile.conf;fi
	@echo "DISABLE_TINYXML = true" >> .Makefile.conf 
disable-curl:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_CURL/d .Makefile.conf;fi
	echo "DISABLE_CURL = true" >> .Makefile.conf 
disable-occi:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_OCCI/d .Makefile.conf;fi
	echo "DISABLE_OCCI = true" >> .Makefile.conf 
disable-cjson:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_CJSON/d .Makefile.conf;fi
	echo "DISABLE_CJSON = true" >> .Makefile.conf 

enable-tinyxml:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_TINYXML/d .Makefile.conf;fi
	echo "DISABLE_TINYXML = false" >> .Makefile.conf 
enable-curl:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_CRUL/d .Makefile.conf;fi
	echo "DISABLE_CRUL = false" >> .Makefile.conf 
enable-occi:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_OCCI/d .Makefile.conf;fi
	echo "DISABLE_OCCI = false" >> .Makefile.conf 
enable-cjson:
	@if [ -f ".Makefile.conf" ];then sed -i /DISABLE_CJSON/d .Makefile.conf;fi
	echo "DISABLE_CJSON = false" >> .Makefile.conf 

DISABLE_TINYXML = $(shell grep -s DISABLE_TINYXML .Makefile.conf|grep true|wc -l| tr -d '[ \t]')
DISABLE_CURL = $(shell grep -s DISABLE_CURL .Makefile.conf|grep true|wc -l| tr -d '[ \t]')
DISABLE_OCCI = $(shell grep -s DISABLE_OCCI .Makefile.conf|grep true|wc -l| tr -d '[ \t]')
DISABLE_CJSON = $(shell grep -s DISABLE_CJSON .Makefile.conf|grep true|wc -l| tr -d '[ \t]')
lrtlibs=
lrt:=$(shell echo "int main() {}"|gcc -x c - -lrt 2>&1)
ifeq ($(lrt),)
  lrtlibs:=-lrt
else
  lrtlibs:=
endif

ifeq ($(TINYXML_LIBRARY),)
TINYXML_LIBRARY := tinyxml
endif

ifeq ($(DISABLE_TINYXML), 1)
FLAG := $(FLAG) -DDISABLE_TINYXML
OBJS := $(filter-out $(OBJS_TINYXML),$(OBJS)) 
else
FLAG := $(FLAG) -DENABLE_TINYXML -l$(TINYXML_LIBRARY)
endif
ifeq ($(CURL_LIBRARY),)
CURL_LIBRARY := curl
endif
ifeq ($(DISABLE_CURL), 1)
FLAG := $(FLAG) -DDISABLE_CURL
OBJS := $(filter-out $(OBJS_CURL),$(OBJS)) 
else
FLAG := $(FLAG) -DENABLE_CURL -l$(CURL_LIBRARY)
endif
ifeq ($(DISABLE_OCCI), 1)
FLAG := $(FLAG) -DDISABLE_OCCI
OBJS := $(filter-out $(OBJS_OCCI),$(OBJS)) 
else
FLAG := $(FLAG) -DENABLE_OCCI
endif
ifeq ($(CJSON_LIBRARY),)
CJSON_LIBRARY := cjson
endif
ifeq ($(DISABLE_CJSON), 1)
FLAG := $(FLAG) -DDISABLE_CJSON
OBJS := $(filter-out $(OBJS_CJSON),$(OBJS)) 
else
FLAG := $(FLAG) -DENABLE_CJSON -l$(CJSON_LIBRARY)
endif

show-objs:
	@echo "禁用功能状态如下："
	@if [ -f ".Makefile.conf" ];then cat .Makefile.conf;fi
	@echo "编译标志："$(FLAG)
	@echo "请仔细查看上述内容，按回车继续编译，Ctrl+c结束编译"
	@read SED


so: $(LIB_OBJECT_SO) 

a: $(LIB_OBJECT_A)

${DIR_SRC}/%.o:${DIR_SRC}/%.cpp
	$(CC) -c $< $(INC_PATH) $(FLAG) $(XJ_FPIC) -o $@

$(LIB_OBJECT_SO): XJ_FPIC = -fPIC

$(LIB_OBJECT_SO): $(OBJS)
	$(CC) $? $(FLAG) $(LIB_PATH) $(INC_PATH) $(SHARED) -o  $(DIR_LIB)/$@ 
	@echo "["$@"]Compile complete"

$(LIB_OBJECT_A): XJ_FPIC = 
$(LIB_OBJECT_A): $(OBJS)
	$(AR) $(DIR_LIB)/$@  $(OBJS)
	@echo "["$@"]Compile complete"

clean:
	@echo remove all object files and lib
	-rm -f $(DIR_LIB)/* $(OBJS) *.o $(DIR_SRC)/*.o .Makefile.conf

demo:
	mkdir -p ./bin
	$(CC) demo_src/XJStringUtilMain.cpp -o bin/mainXJStringUtil  lib/libXJUtilClass.a $(INC_PATH) $(LIB_PATH) -lcurl -lpthread $(lrtlibs)  -lcjson
	$(CC) demo_src/XJJsonBaseMain.cpp -o bin/mainXJJsonBase lib/libXJUtilClass.a $(INC_PATH) $(LIB_PATH) -lcurl -lpthread $(lrtlibs)  -lcjson 
	$(CC) demo_src/XJThreadSpecificMain.cpp demo_src/XJThreadSpecificPlus.cpp  -g -ggdb -o bin/mainXJThreadSpecific  lib/libXJUtilClass.a $(INC_PATH) $(LIB_PATH) -lcurl -lpthread $(lrtlibs)
	$(CC) demo_src/XJUseTimeMain.cpp -o bin/mainXJUseTime lib/libXJUtilClass.a $(INC_PATH) $(LIB_PATH) -lcurl -lpthread $(lrtlibs)  -lcjson 
	$(CC) demo_src/XJMemoryPoolMain.cpp -o bin/mainXJMemoryPool $(INC_PATH) $(LIB_PATH) 
